<?php
namespace Yzadmin\Controller;
use Common\Controller\AdminBaseController;
/**
 * 后台栏目控制器
 */
class CategoryController extends AdminBaseController {

  public function _initialize(){
    parent::_initialize();
    $this->cateType=array('Pages'=>'系统单页','Article'=>'普通文章');
    $this->lists_tpl=array('0'=>array('tpl_name'=>'case','tpl_title'=>'品牌案例'));
    $this->view_tpl=array('0'=>array('tpl_name'=>'about','tpl_title'=>'关于我们'),'1'=>array('tpl_name'=>'advantage','tpl_title'=>'核心优势'));
  }

  /*栏目管理首页*/
  public function index(){
    $lists=D('Category')->formatTree();
    foreach ($lists as $key=>$val){
      $lists[$key]['type']=$this->cateType[$val['type']];
      if ($val['nav_display']==0){
        $lists[$key]['nav_display']='<font class="red-error">不显示</font>';
      }else{
         $lists[$key]['nav_display']='显示';
      }
      if ($val['nav_display']==0){
        $lists[$key]['nav_display']='<font class="red-error">不显示</font>';
      }else{
         $lists[$key]['nav_display']='显示';
      }
    }
    $assign=array(
        'lists'=>$lists,
        'cateType'=>$this->cateType
        );
    $this->assign($assign);
    $this->display();
  }

  /*栏目管理添加*/
  public function add(){
    if (IS_POST){
        $data = I('post.data');
        $result = D('Category')->add($data);
        /*处理单选按钮*/
        if($data['nav_display'] == 'on'){
          $data['nav_display'] = 1;
        }else{
          $data['nav_display'] = 0;
        }/*处理单选按钮*/
        if(false !== $result){
          if ($data['type']=="Pages"){
           $datas['title']=$data['title'];
           $datas['catid']=$result;
           D('Pages')->add($datas);
          }
          $this->success('数据操作成功！',U('index'));exit;
        }else{
          $this->error('服务器正忙，请稍后再试！');
        }
    }
    $catid=I('get.catid');if(empty($catid)){$this->error('非法操作!');}
    $category = D('Category')->find($catid);if(empty($category)){$this->error('信息不存在!');}
    $category_tree = D('Category')->formatTree($map_category);
    $assign=array(
        'catid'=>$category['id'],
        'catename'=>$category['title'],
        'category_tree'=>$category_tree
        );
    $this->assign($assign);
    $this->display('edit');
  }

  /*栏目管理编辑*/
  public function edit(){
    if (IS_POST){
        $data = I('post.data');$id = intval(I('post.id'));
        /*处理单选按钮*/
        if($data['nav_display'] == 'on'){
          $data['nav_display'] = 1;
        }else{
          $data['nav_display'] = 0;
        }/*处理单选按钮*/
        if(empty($id)){$this->error('服务器正忙，请稍后再试！');}
        $result = D('Category')->where(array('id'=>$id))->save($data);
        if(false !== $result){
          //查询单页数据是否存在，如果不存在则新添加
          $about=D('Pages')->where('catid='.$id)->find();
          if (!$about){
            if ($data['type']=="Pages"){
               $datas['title']=$data['title'];
               $datas['catid']=$id;
               D('Pages')->add($datas);
            }
          }else{
               $datas['title']=$data['title'];
               $datas['catid']=$id;
               D('Pages')->where('catid='.$id)->save($datas);
          }
          $this->success('数据操作成功！',U('index'));exit;
        }else{
          $this->error('服务器正忙，请稍后再试！');
        }
    }
    $id=I('get.id');if(empty($id)){$this->error('非法操作!');}
    $info = D('Category')->find($id);if(empty($info)){$this->error('信息不存在!');}
    $category_tree = D('Category')->formatTree($map_category);
    $assign=array(
        'info'=>$info,
        'category_tree'=>$category_tree
        );
    $this->assign($assign);
    $this->display();
  }

  /*删除操作*/
    function del($id = 0){
      if(IS_POST){
        if(empty($id)){echo 0;exit;}
        $category = D('Category')->find($id);if(empty($category)){echo 0;exit;}
        $category_tree = D('Category')->where(array('pid'=>$category['id']))->select();
        if(!empty($category_tree)){echo 2;exit;}
        if($category != 'Pages'){
          $art_result = D('Article')->where('catid='.$category['id'])->delete();
        }else{
          $page_result = D('Pages')->where('catid='.$category['id'])->delete();
        }
        $result = D('Category')->where('id='.$category['id'])->delete();
        if($result){
          echo 1;exit;
        }else{
          echo 0;exit;
        }
      }
    }

}
